/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";

jQuery(function () {

    App.init();

    jQuery('.logOut').attr('href','../logins/login.html');
    var token = sessionStorage.getItem('token');
    if (token == null || token =='') {
        location.href = HOST_FRT+'/logins/login.html';
    }
    var projId = getArgs().id;

    //获取基本信息
    jQuery.ajax({
        url: HOST_ADDR+'/cpmp/bidProjOnMngForPchs/view/'+projId,
        type: 'get',
        // data: JSON.stringify(data),
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,'login_type':'ums'
        },
        success: function(resp){
            if (!resp.success) {
                alert(resp.msgCod+' '+resp.msgInf);
                return;
            }
            var projqotCntTyp = '非实时报价';
            var currTyp = '';
            var dpstFlg = '不需要';
            var pubPriFlg = '否';
            if (resp.data.bidProjOn.qotCntTyp == 2) {
                projqotCntTyp='实时报价';
            }
            if (resp.data.bidProjOn.currTyp == 'USD') {
                currTyp='美元';
            } else if (resp.data.bidProjOn.currTyp == 'RMB') {
                currTyp='人民币';
            }
            if (resp.data.bidProjOn.dpstFlg == 1) {
                dpstFlg='需要';
            }
            if (resp.data.bidProjOn.pubPriFlg == 1) {
                pubPriFlg='是';
            }
            var bidRngTyp = '非定向招标';
            var splrs=[];
            var bidOpenTim = '',bidEndTim = '', projMemo = '',projNbr = '';
            if (resp.data.bidProjOn.bidRngTyp == 1) {
                bidRngTyp='定向招标';
                for (var i=0;i<resp.data.splrs.length;i++) {
                    splrs.push(resp.data.splrs[i].fullNam);
                }
            }
            
            if (resp.data.bidProjOn.bidOpenTim != null&&resp.data.bidProjOn.bidOpenTim !='') {
                 bidOpenTim = (new Date(resp.data.bidProjOn.bidOpenTim)).Format("yyyy-MM-dd hh:mm:ss");
            }
            if (resp.data.bidProjOn.bidEndTim != 'null'&&resp.data.bidProjOn.bidEndTim !='') {
                 bidEndTim = (new Date(resp.data.bidProjOn.bidEndTim)).Format("yyyy-MM-dd hh:mm:ss");
            }
            if (resp.data.bidProjOn.projMemo != null&&resp.data.bidProjOn.projMemo !='') {
                 projMemo = resp.data.bidProjOn.projMemo;
            }
            if (resp.data.bidProjOn.projNbr != null &&resp.data.bidProjOn.projNbr !='') {
                projNbr=resp.data.bidProjOn.projNbr;
            }
            var matTyp = resp.data.bidProjOn.matTyp;
            if(resp.data.matTypDesc!=null && resp.data.matTypDesc!=''){
                matTyp = resp.data.matTypDesc;
            }

            var atches = resp.data.atches;
            if (atches.length > 0){
                for (var i= 0;i<atches.length;i++) {
                    jQuery('#tab_3').append('<div class="controls atchDiv"><span src="'+atches[i].atchUrl+'">'+atches[i].atchNam+'</span>' +
                        '&emsp;<a target="_blank" href="'+HOST_FILE+atches[i].atchUrl+'">查看</a></div>');
                }
            }

            // console.log(resp)
            jQuery('.basicInfo').append('<div class="portlet-body"><table class="table table-striped table-bordered"><tbody><tr>' +
                '<td width="16%">项目名称：</td><td colspan="2">'+resp.data.bidProjOn.projNam+'</td> ' +
                '<td width="16%">项目编号：</td> <td colspan="2">'+projNbr+'</td></tr> <tr><tr>' +
                '<td width="16%">物料品类：</td><td colspan="2">'+matTyp+'</td>' +
                '<td width="16%">报价次数类型：</td><td colspan="2">'+projqotCntTyp+'</td></tr> <tr>' +
                '<td width="16%">招标单位：</td><td colspan="2">'+resp.data.bidProjOn.bidDptNam+'</td>' +
                '<td width="16%">招标单位地址：</td><td colspan="2">'+resp.data.bidProjOn.bidDptAddr+'</td></tr><tr>' +
                '<tr><td width="16%">是否需要投标保证金：</td><td colspan="2">'+dpstFlg+'</td>'+
                '<td width="16%">是否需要公开招标价格：</td><td colspan="2">'+pubPriFlg+'</td></tr>'+
                '<tr><td width="16%">投标保证金（元）：</td><td colspan="2">'+resp.data.bidProjOn.bidDpst+'</td>' +
                '<td width="16%">币种：</td><td colspan="2">'+currTyp+'</td></tr>'+
                '<tr><td width="16%">联系人：</td><td colspan="2">'+resp.data.bidProjOn.ctct+'</td>' +
                '<td width="16%">联系电话：</td><td colspan="2">'+resp.data.bidProjOn.ctctTel+'</td></tr><tr>' +
                '<td width="16%">投标开标时间：</td><td colspan="2">'+bidOpenTim+'</td>' +
                '<td width="16%">投标截止时间：</td><td colspan="2">'+bidEndTim+'</td></tr><tr>' +
                '<td>招标范围类型：</td><td colspan="2">'+bidRngTyp+'</td>' +
                '<td width="16%">指定招标供应商：</td><td colspan="2">'+splrs+'</td></tr></tr><tr>' +
                '<td>备注信息：</td><td colspan="5">'+projMemo+'</td></tr></tbody></table></div>');
        },
        error:function(err){
            console.log(err);
        }
    });

    //获取专家
    var data = {};
    jQuery.ajax({
        url: HOST_ADDR+'/cpmp/bidProjOnMngForPchs/getExptInfs',
        type: 'post',
        data: JSON.stringify(data),
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,'login_type':'ums'
        },
        success: function(resp){
            if (!resp.success) {
                alert(resp.msgCod+' '+resp.msgInf);
                return;
            }
            // console.log(resp)

            for (var i = 0;i< resp.data.length;i++) {
                jQuery('.chooseExpert').append('<label class="checkbox"><span><input type="checkbox" value="'+resp.data[i].exptId+'"></span>'+resp.data[i].exptNam+'</label>');
            }
        },
        error:function(err){
            console.log(err);
        }
    });
    
    //提交专家
    jQuery('.expertOk').click(function () {
        let experts = '';
        for(var i=0; i<jQuery('.chooseExpert input').length; i++){
            if(jQuery('.chooseExpert input')[i].checked) experts+=jQuery('.chooseExpert input')[i].value+',';
        }
        if (experts.length == 0) {
            alert('请选择项目评标专家！');
            return;
        }
        var data = {
            "projId":projId,
            "exptIds":experts
        };
        console.log(data)

        jQuery.ajax({
            url: HOST_ADDR+'/cpmp/bidProjOnMngForPchs/chsExpts',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json',
            contentType:"application/json",
        headers: {
            'access_token': token,'login_type':'ums'
        },
            success: function(resp){
                if (!resp.success) {
                    alert(resp.msgCod+' '+resp.msgInf);
                    return;
                }
                console.log(resp)
                alert('选择专家成功！')
                location.href = '/bid_online/proj_list.html';
            },
            error:function(err){
                console.log(err);
            }
        });
        
    });

});

